package org.jiuling.xml.read;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * 用于XML的简单API
 * @author 紫灵
 * @date 2024年01月27日 16时29分19秒
 */
public class SimpleAPIForXML {

    public static void main(String[] args) throws Exception {
        //1.或去SAXParserFactory实例
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.获取SAXparser实例
        SAXParser saxParser = factory.newSAXParser();
        //创建Handel对象
        SimpleAPIForXML.SAXHandel handel = new SimpleAPIForXML.SAXHandel();
        saxParser.parse("D:\\abx.xml",handel);
    }

    public static class SAXHandel extends DefaultHandler {
        //遍历xml文件开始标签
        @Override
        public void startDocument() throws SAXException {
            super.startDocument();
            System.out.println("sax解析开始");
        }

        //遍历xml文件结束标签
        @Override
        public void endDocument() throws SAXException {
            super.endDocument();
            System.out.println("sax解析结束");
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            super.startElement(uri, localName, qName, attributes);
            if (qName.equals("book")){
                System.out.println("============开始遍历student=============");
                //System.out.println(attributes.getValue("rollno"));
            }
            else if (!qName.equals("book")&&!qName.equals("bookstore")){
                System.out.print("节点名称:"+qName+"----");
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(uri, localName, qName);
            if (qName.equals("book")){
                System.out.println(qName+"遍历结束");
                System.out.println("============结束遍历student=============");
            }else{
                System.out.println(qName+"遍历结束");
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            super.characters(ch, start, length);
            String value = new String(ch,start,length).trim();
            if (!value.equals("")) {
                System.out.println(value);
            }
        }
    }

}


